package cn.edu.tju.cas.dao.impl;

import cn.edu.tju.cas.dao.UserDao;
import cn.edu.tju.cas.dao.UserGroupApplyDao;
import cn.edu.tju.cas.entity.UserEntity;
import cn.edu.tju.cas.entity.UserGroupApplyEntity;
import org.hibernate.HibernateException;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.orm.hibernate4.HibernateCallback;
import org.springframework.stereotype.Repository;

import java.sql.SQLException;
import java.util.List;

/**
 * Created by catold on 16/2/20.
 */
@Repository("userGroupApplyDao")
public class UserGroupApplyDaoImpl extends BaseDaoImpl<UserGroupApplyEntity> implements UserGroupApplyDao{

    public UserGroupApplyDaoImpl(){
        super();

        setClass(UserGroupApplyEntity.class);
    }


    @Override
    public List<UserGroupApplyEntity> queryByState(int state) {
        return (List<UserGroupApplyEntity>) this.getHibernateTemplate().find("from UserGroupApplyEntity where status=?", state);
    }

    @Override
    public List<UserGroupApplyEntity> queryByUserAndState(int userId,int state) {
        return (List<UserGroupApplyEntity>) this.getHibernateTemplate().find("from UserGroupApplyEntity where userEntity.userId = ? and status=?",userId, state);
    }

//    @Override
//    public List<Integer> getUserGroupIds(int userId) {
//        return (List<Integer>)getHibernateTemplate().find("select groupId from UserGroupApplyEntity where userId = ?",userId);
//    }

    @Override
    public List<UserGroupApplyEntity> getUserApply(int userId) {
        return (List<UserGroupApplyEntity>)getHibernateTemplate().find("from UserGroupApplyEntity where userEntity.userId = ?",userId);
    }

    @Override
    public UserGroupApplyEntity getByUserIdAndGroupId(int userId, int groupId) {
        return getOneFromList(getHibernateTemplate().find("from UserGroupApplyEntity where userEntity.userId = ? and groupEntity.groupId = ?",userId, groupId));
    }

    @Override
    public List<UserGroupApplyEntity> getUserApplyByGroupId(int groupId) {
        return (List<UserGroupApplyEntity>)getHibernateTemplate().find("from UserGroupApplyEntity where groupEntity.groupId = ?",groupId);
    }



}
